#!/bin/bash

source /home/shell/util.sh

apt update
apt install -y build-essential libtool autotools-dev automake pkg-config bsdmainutils python3
apt install -y libevent-dev libboost-dev libboost-system-dev libboost-filesystem-dev libboost-test-dev
apt install -y libsqlite3-dev
apt install -y libminiupnpc-dev libnatpmp-dev
apt install -y libzmq3-dev
apt install -y libqt5gui5 libqt5core5a libqt5dbus5 qttools5-dev qttools5-dev-tools
apt install -y libqrencode-dev

# 手动安装Oracle berkeley-db
# download /opt/berkeley-db.tar.gz http://download.oracle.com/berkeley-db/db-4.8.30.NC.tar.gz
download /opt/berkeley-db.tar.gz http://download.oracle.com/berkeley-db/db-18.1.40.tar.gz
tar -xf /opt/berkeley-db.tar.gz -C /opt
rm -rf /opt/berkeley-db.tar.gz
mv /opt/db-* /opt/berkeley-db

cd /opt/berkeley-db/build_unix
../dist/configure --enable-cxx --disable-shared --with-pic --prefix=/usr/local
make -j8
make install

# 手动安装libevent
apt update
apt install -y build-essential libtool autotools-dev autoconf pkg-config libssl-dev
apt install -y libboost-all-dev
apt install -y libqt5gui5 libqt5core5a libqt5dbus5 qttools5-dev qttools5-dev-tools libprotobuf-dev protobuf-compiler
apt install -y libqrencode-dev
apt install -y libminiupnpc-dev

download /usr/local/src/libevent.stable.tar.gz https://ghproxy.com/https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
tar -xf /usr/local/src/libevent.stable.tar.gz -C  /usr/local/src
rm -rf /usr/local/src/libevent.stable.tar.gz
mv /usr/local/src/libevent-*-stable /usr/local/src/libevent-stable
cd /usr/local/src/libevent-stable
./configure -prefix=/usr
make && make install

# 安装BTC
git clone https://gitee.com/mirrors/bitcoin.git /usr/local/src/bitcoin

cd /usr/local/src/bitcoin
git branch -a
read -p "选择版本[0.21]：" bitcoin_version
bitcoin_version=${bitcoin_version:-"0.21"};
git checkout -b origin/${bitcoin_version} remotes/origin/${bitcoin_version}
./autogen.sh
./configure --with-incompatible-bdb --prefix=/usr/local/bitcoin --enable-wallet
make && make install

if [ ! -d "/home/bitcoin/" ]; then
    mkdir -p /home/bitcoin/ 
fi

tee /etc/profile.d/bitcoin.sh<<EOF
export PATH=\$PATH:/usr/local/bitcoin/bin
EOF
source /etc/profile.d/bitcoin.sh

tee ~/.bitcoin/bitcoin.conf<<EOF
rpcuser=admin
rpcpassword=admin123
EOF

cp -r /home/shell/support/btc/bitcoin.conf /home/bitcoin/

bitcoind -conf=/home/bitcoin/bitcoin.conf -datadir=/home/bitcoin/ -daemon

firewall-addport 18332
firewall-addport 18443